package com.kj.platformlogistics.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kj.platformlogistics.R;
import com.kj.platformlogistics.base.BaseActivity;
import com.kj.platformlogistics.constants.UrlConstants;
import com.kj.platformlogistics.home.adapter.HomeHistoryOrderAdapter;
import com.kj.platformlogistics.home.model.HomeHistoryOrderList;
import com.kj.platformlogistics.http.HttpRequest;
import com.kj.platformlogistics.http.HttpStringCallBack;
import com.kj.platformlogistics.util.JsonUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.json.JSONException;
import org.json.JSONObject;

public class SearchOrderListActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView rcView;
    private SmartRefreshLayout refresh;
    private LinearLayout llNodata;
    private HomeHistoryOrderAdapter adapter;
    private EditText etCode;
    private String code="";

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, SearchOrderListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_historyorder_list;
    }

    @Override
    protected void initView() {
        setTitleText("订单搜索");
        etCode=findViewById(R.id.etCode);
        refresh=findViewById(R.id.refresh);
        rcView=findViewById(R.id.rcCommon);
        llNodata=findViewById(R.id.llNodata);

        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                code=s.toString().trim();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etCode.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //是否是回车键
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    //隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(SearchOrderListActivity.this.getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    //搜索
                    getData();
                }
                return false;
            }
        });
    }

    @Override
    protected void initData() {

    }

    private void getData() {
        new HttpRequest(SearchOrderListActivity.this).doPost(UrlConstants.logistics_getOrderByCode+code, "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                try {
                    String data = result.toString();
                    //把请求得数据转换成json对象
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), HomeHistoryOrderList.class);
                        if (obj instanceof HomeHistoryOrderList) {
                            HomeHistoryOrderList model = (HomeHistoryOrderList) obj;
                            if (model.getResult().size()>0){
                                rcView.setVisibility(View.VISIBLE);
                                llNodata.setVisibility(View.GONE);
                                adapter=new HomeHistoryOrderAdapter(model.getResult());
                                rcView.setLayoutManager(new LinearLayoutManager(SearchOrderListActivity.this));
                                rcView.setAdapter(adapter);
                            }else {
                                rcView.setVisibility(View.GONE);
                                llNodata.setVisibility(View.VISIBLE);
                            }
                        }
                    }else {
                        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            }
        });
    }


    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return 0;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }
}
